#include <QtGui>
#include <QtCore>
#include "MainWindow.h" 
#include "MatrixSerialize.h"
#define _NEED_CONSOLE_OUTPUT
#ifdef _NEED_CONSOLE_OUTPUT
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <iostream>

void render_qt_text(QPainter *painter, int w, int h, const QColor &color) {
	QPainterPath path;
	path.moveTo(-0.083695, 0.283849);
	path.cubicTo(-0.049581, 0.349613, -0.012720, 0.397969, 0.026886, 0.428917);
	path.cubicTo(0.066493, 0.459865, 0.111593, 0.477595, 0.162186, 0.482108);
	path.lineTo(0.162186, 0.500000);
	path.cubicTo(0.115929, 0.498066, 0.066565, 0.487669, 0.014094, 0.468810);
	path.cubicTo(-0.038378, 0.449952, -0.088103, 0.423839, -0.135082, 0.390474);
	path.cubicTo(-0.182061, 0.357108, -0.222608, 0.321567, -0.256722, 0.283849);
	path.cubicTo(-0.304712, 0.262250, -0.342874, 0.239362, -0.371206, 0.215184);
	path.cubicTo(-0.411969, 0.179078, -0.443625, 0.134671, -0.466175, 0.081963);
	path.cubicTo(-0.488725, 0.029255, -0.500000, -0.033043, -0.500000, -0.104932);
	path.cubicTo(-0.500000, -0.218407, -0.467042, -0.312621, -0.401127, -0.387573);
	path.cubicTo(-0.335212, -0.462524, -0.255421, -0.500000, -0.161752, -0.500000);
	path.cubicTo(-0.072998, -0.500000, 0.003903, -0.462444, 0.068951, -0.387331);
	path.cubicTo(0.133998, -0.312218, 0.166522, -0.217440, 0.166522, -0.102998);
	path.cubicTo(0.166522, -0.010155, 0.143394, 0.071325, 0.097138, 0.141441);
	path.cubicTo(0.050882, 0.211557, -0.009396, 0.259026, -0.083695, 0.283849);
	path.moveTo(-0.167823, -0.456963);
	path.cubicTo(-0.228823, -0.456963, -0.277826, -0.432624, -0.314831, -0.383946);
	path.cubicTo(-0.361665, -0.323340, -0.385082, -0.230335, -0.385082, -0.104932);
	path.cubicTo(-0.385082, 0.017569, -0.361376, 0.112025, -0.313964, 0.178433);
	path.cubicTo(-0.277248, 0.229368, -0.228534, 0.254836, -0.167823, 0.254836);
	path.cubicTo(-0.105088, 0.254836, -0.054496, 0.229368, -0.016045, 0.178433);
	path.cubicTo(0.029055, 0.117827, 0.051605, 0.028691, 0.051605, -0.088975);
	path.cubicTo(0.051605, -0.179562, 0.039318, -0.255803, 0.014744, -0.317698);
	path.cubicTo(-0.004337, -0.365409, -0.029705, -0.400548, -0.061362, -0.423114);
	path.cubicTo(-0.093018, -0.445680, -0.128505, -0.456963, -0.167823, -0.456963);
	path.moveTo(0.379011, -0.404739);
	path.lineTo(0.379011, -0.236460);
	path.lineTo(0.486123, -0.236460);
	path.lineTo(0.486123, -0.197292);
	path.lineTo(0.379011, -0.197292);
	path.lineTo(0.379011, 0.134913);
	path.cubicTo(0.379011, 0.168117, 0.383276, 0.190442, 0.391804, 0.201886);
	path.cubicTo(0.400332, 0.213330, 0.411246, 0.219052, 0.424545, 0.219052);
	path.cubicTo(0.435531, 0.219052, 0.446227, 0.215264, 0.456635, 0.207689);
	path.cubicTo(0.467042, 0.200113, 0.474993, 0.188910, 0.480486, 0.174081);
	path.lineTo(0.500000, 0.174081);
	path.cubicTo(0.488436, 0.210509, 0.471957, 0.237911, 0.450564, 0.256286);
	path.cubicTo(0.429170, 0.274662, 0.407054, 0.283849, 0.384215, 0.283849);
	path.cubicTo(0.368893, 0.283849, 0.353859, 0.279094, 0.339115, 0.269584);
	path.cubicTo(0.324371, 0.260074, 0.313530, 0.246534, 0.306592, 0.228965);
	path.cubicTo(0.299653, 0.211396, 0.296184, 0.184075, 0.296184, 0.147002);
	path.lineTo(0.296184, -0.197292);
	path.lineTo(0.223330, -0.197292);
	path.lineTo(0.223330, -0.215667);
	path.cubicTo(0.241833, -0.224049, 0.260697, -0.237992, 0.279922, -0.257495);
	path.cubicTo(0.299147, -0.276999, 0.316276, -0.300129, 0.331310, -0.326886);
	path.cubicTo(0.338826, -0.341070, 0.349523, -0.367021, 0.363400, -0.404739);
	path.lineTo(0.379011, -0.404739);
	path.moveTo(-0.535993, 0.275629);

	painter->translate(w / 2, h / 2);
	double scale = qMin(w, h) * 8 / 10.0;
	painter->scale(scale, scale);

	painter->setRenderHint(QPainter::Antialiasing);

	painter->save();
	painter->translate(.1, .1);
	painter->fillPath(path, QColor(0, 0, 0, 63));
	painter->restore();

	painter->setBrush(color);
	painter->setPen(QPen(Qt::black, 0.02, Qt::SolidLine, Qt::FlatCap, Qt::RoundJoin));
	painter->drawPath(path);
}


BOOL CreateConsole(void)
{
	FreeConsole();        
	if ( AllocConsole() )
	{
		int hCrt = _open_osfhandle((long)
			GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
		*stdout = *(::_fdopen(hCrt, "w"));
		::setvbuf(stdout, NULL, _IONBF, 0);
		*stderr = *(::_fdopen(hCrt, "w"));
		::setvbuf(stderr, NULL, _IONBF, 0);
		return TRUE;
	}
	return FALSE;
}
#endif
#define  MAX_CORNERS 100
int main(int argc, char * argv[])
{

	 
#ifdef _NEED_CONSOLE_OUTPUT
	 CreateConsole();
#endif // _NEED_CONSOLE_OUTPUT
	 //IplImage* pImg = COpenCVSerializer::Load("1.txt");
	 
	 Q_INIT_RESOURCE(application);
	 QApplication app(argc, argv);
	 QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
	 OCEANUI::CMainWindow mainWin;
     mainWin.show();
	 return  app.exec();
	

	//IplImage* pFrame = NULL;
 //   CvCapture* pCapture = cvCreateFileCapture("test.avi");
	//int frameIindex = 0;
	//IplImage* imgARaw = NULL;// cvLoadImage("OpticalFlow0.jpg",CV_LOAD_IMAGE_GRAYSCALE);
	//IplImage* imgBRaw = NULL;//cvLoadImage("OpticalFlow1.jpg",CV_LOAD_IMAGE_GRAYSCALE);
	//IplImage* imgA = NULL;// cvLoadImage("OpticalFlow0.jpg",CV_LOAD_IMAGE_GRAYSCALE);
	//IplImage* imgB = NULL;//cvLoadImage("OpticalFlow1.jpg",CV_LOAD_IMAGE_GRAYSCALE);
	//double fps = cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS);
	//int waitTime = cvRound((1/fps) * 1000);
	//while(imgBRaw = cvQueryFrame( pCapture ))
	//{
	//	CvSize      img_sz    = cvGetSize( imgBRaw );
	//	imgB = cvCreateImage(img_sz,8,1);
	//	cvCvtColor(imgBRaw,imgB,CV_RGB2GRAY);
	//	++frameIindex;
	//    if (1 == frameIindex)
	//	{
 //            imgARaw = imgBRaw; 
	//		 imgA = imgB;
	//		continue;
	//	}

	//	
	//	int         win_size = 10;
	//	IplImage* imgC = imgA;//cvLoadImage("OpticalFlow1.jpg",CV_LOAD_IMAGE_UNCHANGED);

	//	// The first thing we need to do is get the features
	//	// we want to track.
	//	//
	//	IplImage* eig_image = cvCreateImage( img_sz, IPL_DEPTH_32F, 1 );
	//	IplImage* tmp_image = cvCreateImage( img_sz, IPL_DEPTH_32F, 1 );
	//	int              corner_count = MAX_CORNERS;
	//	CvPoint2D32f* cornersA        = new CvPoint2D32f[ MAX_CORNERS ];
	//	cvGoodFeaturesToTrack(
	//		imgA,
	//		eig_image,
	//		tmp_image,
	//		cornersA,
	//		&corner_count,
	//		0.01,
	//		5.0,
	//		0,
	//		3,
	//		0,
	//		0.04
	//		);
	//	cvFindCornerSubPix(
	//		imgA,
	//		cornersA,
	//		corner_count,
	//		cvSize(win_size,win_size),
	//		cvSize(-1,-1),
	//		cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03)
	//		);
	//	// Call the Lucas Kanade algorithm
	//	//
	//	char features_found[ MAX_CORNERS ];
	//	float feature_errors[ MAX_CORNERS ];
	//	CvSize pyr_sz = cvSize( imgA->width+8, imgB->height/3 );
	//	IplImage* pyrA = cvCreateImage( pyr_sz, IPL_DEPTH_32F, 1 );
	//	IplImage* pyrB = cvCreateImage( pyr_sz, IPL_DEPTH_32F, 1 );
	//	CvPoint2D32f* cornersB        = new CvPoint2D32f[ MAX_CORNERS ];
	//	cvCalcOpticalFlowPyrLK(
	//		imgA,
	//		imgB,
	//		pyrA,
	//		pyrB,
	//		cornersA,
	//		cornersB,
	//		corner_count,
	//		cvSize( win_size,win_size ),
	//		5,
	//		features_found,
	//		feature_errors,
	//		cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, .3 ),
	//	 0
	//	 );
	//	// Now make some image of what we are looking at:
	//	//
	//	for( int i=0; i<corner_count; i++ ) {
	//		if( features_found[i]==0|| feature_errors[i]>550 ) {
	//			//       printf("Error is %f/n",feature_errors[i]);
	//			continue;
	//	 }
	//		std::cout<< "Got it/n" <<std::endl;
	//		CvPoint p0 = cvPoint(
	//			cvRound( cornersA[i].x ),
	//			cvRound( cornersA[i].y )
	//		 );
	//		CvPoint p1 = cvPoint(
	//			cvRound( cornersB[i].x ),
	//			cvRound( cornersB[i].y )
	//		 );
	//		cvLine( imgARaw, p0, p1, CV_RGB(255,0,0),1 );
	//	}
	//	cvNamedWindow("ImageA",0);
	//	cvNamedWindow("ImageB",0);
	//	cvNamedWindow("LKpyr_OpticalFlow",0);
	//	cvShowImage("ImageA",imgA);
	//	cvShowImage("ImageB",imgB);
	//	cvShowImage("LKpyr_OpticalFlow",imgARaw);
	//	cvWaitKey(waitTime);
	//	imgARaw = imgBRaw; 
	//	imgA = imgB;
	//}

	// return 0;
}



